<template>
  <el-dialog :close-on-click-modal="false"
             :visible.sync="visible"
             @close="close_dialog"
             :show-close="false"
             class="custom_dialog_box"
             width="30%">
    <div slot="title"
         style="display: flex;justify-content: space-between;">
      <div style="font-size:18px">{{dialogType == 'add' ? '新增' : dialogType == 'edit' ? '编辑' : '查看'}}</div>
      <div>
        <el-button size="mini"
                   type="info"
                   plain
                   @click="close_dialog">关闭</el-button>
        <el-button type="primary"
                   size="mini"
                   @click="handle_add"
                   v-if="dialogType == 'add'"
                   plain>确认新增</el-button>
        <el-button type="primary"
                   size="mini"
                   @click="handle_edit"
                   v-if="dialogType == 'edit'"
                   plain>确认修改</el-button>
      </div>
    </div>
    <el-row>
      <el-form :model="form_data"
               :rules="form_rules"
               ref="rule_form"
               label-width="60px"
               size="mini">
        <el-col >
          <el-form-item label="名称"
                        prop="name">
            <el-input v-model="form_data.name"
                      style="width: 100%"
                      maxlength="200"
                      :disabled="is_disabled"
                      placeholder="必填: "></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="地址"
                        prop="url">
            <el-input v-model="form_data.url"
                      style="width: 100%"
                      maxlength="200"
                      :disabled="is_disabled"
                      placeholder="必填: "></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="类型"
                        prop="type">
            <el-input v-model="form_data.type"
                      maxlength="50"
                      :disabled="is_disabled"
                      placeholder="必填: "></el-input>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="颜色"
                        prop="color">
            <el-input v-model="form_data.color"
                      maxlength="100"
                      :disabled="is_disabled"
                      placeholder="必填: "></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>

export default {
  data () {
    return {
      visible: false,
      dialogType: "",
      form_data: {
        name: '', 
        url: '',  
        type: "",  
        color: ""
      },

      form_rules: {
        name: [
          { required: true, message: '必填: ', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '必填: ', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '必填: ', trigger: 'blur' }
        ],
        color: [
          { required: true, message: '必填: ', trigger: 'blur' }
        ],
      },
      is_disabled: false,
    }
  },
  methods: {
    // 初始化
    init (id, type) {
      this.dialogType = type;
      this.visible = true;

      if(type == 'add') {

      }

      if (type == 'view') {   // 查看
        this.is_disabled = true;
        this.getInfo(id);
      };
      if (type == 'edit') {   // 修改
        this.getInfo(id);
      };

     
      this.$nextTick(() => {
        this.$refs['rule_form'].resetFields();
      });
    },

  

    // 接口: 新增
    handle_add () {
      this.$refs['rule_form'].validate((valid) => {
        if (valid) {
          this.$http({
              url: this.$http.adornUrl(`/banner/save`),
              method: 'post',
              data: this.$http.adornData({
                'name': this.form_data.name,
                'url': this.form_data.url,
                'type': this.form_data.type,
                'color': this.form_data.color
              })
            }).then(({data}) => {
              console.log("data:", data);
              if(data.code == 0) {
                this.$emit("close");
                this.$emit("updateTable");
              }
            })
        } else {
          this.$message.error('请填写完整表单')
        }
      })
    },

    // 接口: 修改
    handle_edit () {
      this.$refs['rule_form'].validate((valid) => {
        if (valid) {
          this.$http({
              url: this.$http.adornUrl(`/banner/update`),
              method: 'post',
              data: this.$http.adornData({
                'bannerId': this.form_data.bannerId,
                'name': this.form_data.name,
                'url': this.form_data.url,
                'type': this.form_data.type,
                'color': this.form_data.color
              })
            }).then(({data}) => {
              console.log("data:", data);
              if(data.code == 0) {
                this.$emit("close");
                this.$emit("updateTable");
              }
            })
        } else {
          this.$message.error('请填写完整表单')
        }
      })
    },

    // 详情
    getInfo (bannerId) {
      this.$http({
        url: this.$http.adornUrl(`/banner/info/${bannerId}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data.code == 0) {
          if(data.code == 0) {
            this.form_data = data.banner;
          }
        }
      })
    },

    // 关闭模态框
    close_dialog () {
      this.$emit("close");
    }
  }
}
</script>

<style lang="scss" scoped>


/deep/ .is-disabled {
  .el-input__inner {
    background-color: #fff !important;
    color: #303133 !important;
  }
  .el-textarea__inner {
    background-color: #fff !important;
    color: #303133 !important;
  }
  .el-radio__inner {
    background-color: #fff !important;
  }
  .el-checkbox__inner {
    background-color: #fff !important;
  }
  .el-checkbox__label {
    color: #303133 !important;
  }
  .el-radio__label {
    color: #303133 !important;
  }
  .el-range-input {
    background-color: #fff !important;
    color: #303133 !important;
  }
}






</style>